package org.jeecg.modules.wms.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.wms.entity.WmsInboundOrderDetail;
import org.jeecg.modules.wms.entity.WmsWarehouseLocation;
import org.jeecg.modules.wms.entity.WmsWarehouse;
import org.jeecg.modules.wms.mapper.WmsInboundOrderDetailMapper;
import org.jeecg.modules.wms.mapper.WmsWarehouseLocationMapper;
import org.jeecg.modules.wms.mapper.WmsWarehouseMapper;
import org.jeecg.modules.wms.service.IWmsInboundOrderDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.List;

/**
 * 入库单明细服务实现
 */
@Service
public class WmsInboundOrderDetailServiceImpl extends ServiceImpl<WmsInboundOrderDetailMapper, WmsInboundOrderDetail> implements IWmsInboundOrderDetailService {
    @Autowired
    private WmsWarehouseLocationMapper locationMapper;
    
    @Autowired
    private WmsWarehouseMapper warehouseMapper;

    @Override
    public List<WmsInboundOrderDetail> listByInboundOrderId(String inboundOrderId) {
        return baseMapper.selectByInboundOrderId(inboundOrderId);
    }

    @Override
    public List<WmsInboundOrderDetail> listByInboundOrderIdAndUpdateBy(String inboundOrderId,String username) {
        return baseMapper.selectByInboundOrderIdAndUpdateBy(inboundOrderId,username);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchSaveDetails(List<WmsInboundOrderDetail> details) {
        if (details == null || details.isEmpty()) {
            return true;
        }
        
        // 使用批量插入提高效率
        return baseMapper.batchInsert(details) > 0;
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteByInboundOrderId(String inboundOrderId) {
        if (inboundOrderId == null || inboundOrderId.isEmpty()) {
            return false;
        }
        
        return baseMapper.deleteByInboundOrderId(inboundOrderId) >= 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateDetail(WmsInboundOrderDetail detail) {
        if (detail == null || detail.getId() == null) {
            return false;
        }
        
        // 查询并设置仓库名称和库位名称
        if (detail.getPlanWarehouseId() != null) {
            WmsWarehouse warehouse = warehouseMapper.selectById(detail.getPlanWarehouseId());
            if (warehouse != null) {
                detail.setPlanWarehouseName(warehouse.getWarehouseName());
            }
        }
        
        if (detail.getPlanLocationId() != null) {
            WmsWarehouseLocation location = locationMapper.selectById(detail.getPlanLocationId());
            if (location != null) {
                detail.setPlanLocationName(location.getLocationName());
            }
        }
        
        return updateById(detail);
    }
    
    @Override
    public List<WmsInboundOrderDetail> listByInboundOrderIdAndMaterialCode(String inboundOrderId, String materialCode) {
        LambdaQueryWrapper<WmsInboundOrderDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmsInboundOrderDetail::getInboundOrderId, inboundOrderId)
                   .eq(WmsInboundOrderDetail::getMaterialCode, materialCode);
        return this.list(queryWrapper);
    }
} 